/*
 * timer.c
 *
 *  Created on: 2016年9月3日
 *      Author: pengzhikang
 */

#include "naskfunc.h"
#include "int.h"
#include "timer.h"
#include "mtask.h"

#define PIT_CTRL 0x0043
#define PIT_CNT0 0x0040
#define TIMER_NO_USE 0 // 未使用
#define TIMER_FLAGS_ALLOC 1 // 已配置状态
#define TIMER_FLAGS_USING 2 //定时器运行中
struct TIMERCTL timerctl;

extern struct TIMER *task_timer;

void init_pit(void) {
	int i;
	struct TIMER *t;
	io_out8(PIT_CTRL, 0x34);
	// 写入2e9c,先低后高
	io_out8(PIT_CNT0, 0x9c);
	io_out8(PIT_CNT0, 0x2e);
	timerctl.count = 0;
	for (i = 0; i < MAX_TIMER; i++) {
		timerctl.timers0[i].flags = TIMER_NO_USE; /* 未使用 */
	}
	t = timer_alloc();
	t->timeout = 0xffffffff;
	t->flags = TIMER_FLAGS_USING;
	t->next = 0; // 只有一个值的链表
	timerctl.start = t; // 保存链表的头指针
	timerctl.next = 0xffffffff; // 下一个要超时的时刻
}

/**
 * 创建新的定时器并返回指向该定时器结构的指针
 */
struct TIMER *timer_alloc(void) {
	int i;
	for (i = 0; i < MAX_TIMER; ++i) {
		if (timerctl.timers0[i].flags == TIMER_NO_USE) {
			timerctl.timers0[i].flags = TIMER_FLAGS_ALLOC;
			timerctl.timers0[i].flags2 = 0;
			return &timerctl.timers0[i];
		}
	}
	return 0;
}

/**
 * 释放timer所指向的定时器
 */
void timer_free(struct TIMER *timer) {
	timer->flags = TIMER_NO_USE;
}

/**
 * 定时器结构的初始化
 */
void timer_init(struct TIMER *timer, struct FIFO32 *fifo, int data) {
	timer->fifo = fifo;
	timer->data = data;
}

/**
 * 定时器的设定
 */
void timer_settime(struct TIMER *timer, unsigned int timeout) {
	int e;
	struct TIMER *t, *s;
	timer->timeout = timeout + timerctl.count;
	timer->flags = TIMER_FLAGS_USING;
	e = io_load_eflags();
	io_cli();
	t = timerctl.start;
	if (timer->timeout <= t->timeout) {
		timerctl.start = timer;
		timer->next = t;
		timerctl.next = timer->timeout;
		io_store_eflags(e);
		return;
	}
	for (;;) {
		s = t;
		t = t->next;
		if (timer->timeout <= t->timeout) { //插到s和t之间
			s->next = timer;
			timer->next = t;
			io_store_eflags(e);
			return;
		}
	}
}

/**
 * IRQ0发生时调用的中断处理程序
 */
void inthandler20(int *esp) {
	struct TIMER *timer;
	char ts = 0;

	io_out8(PIC0_OCW2, 0x60);
	timerctl.count++;
	if (timerctl.next > timerctl.count) {
		return;
	}

	timer = timerctl.start;
	for (;;) {
		// 定时器处于动作中
		if (timer->timeout > timerctl.count) {
			break;
		}
		// 超时
		timer->flags = TIMER_FLAGS_ALLOC;
		if (timer != task_timer) {
			fifo32_put(timer->fifo, timer->data);
		} else {
			ts = 1;
		}
		timer = timer->next; //链表结构
	}
	//新位移
	timerctl.start = timer;
	timerctl.next = timer->timeout; //将要超时的
	if (ts != 0) {
		task_switch();
	}
}

/**
 * 关闭指定的定时器
 */
int timer_cancel(struct TIMER *timer) {
	int e;
	struct TIMER *t;
	e = io_load_eflags();
	io_cli(); //在设置过程中禁止改变定时器的状态
	if (timer->flags == TIMER_FLAGS_USING) {
		if (timer == timerctl.start) { //第一个要取消的定时器
			t = timer->next;
			timerctl.start = t;
			timerctl.next = t->timeout;
		} else {
			// 找个当前定时器的前一个定时器
			t = timerctl.start;
			for (;;) {
				if (t->next == timer) {
					break;
				}
				t = t->next;
			}
			// a --> b --> c
			// a --> c
			t->next = timer->next;
		}
		timer->flags = TIMER_FLAGS_ALLOC;
		io_store_eflags(e);
		return 1; // 成功取消
	}
	io_store_eflags(e);
	return 0; //不需要取消
}

/**
 * 关闭所有没有使用并由应用程序启动的定时器
 */
void timer_cancelall(struct FIFO32 *fifo) {
	int e,i;
	struct TIMER *t;
	e = io_load_eflags();
	io_cli();
	for(i =0; i < MAX_TIMER; i++) {
		t = &timerctl.timers0[i];
		if(t->flags != TIMER_NO_USE && t->flags2 != 0 && t->fifo == fifo) {
			timer_cancel(t);
			timer_free(t);
		}
 	}
	io_store_eflags(e);
}











